﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using BLL;


namespace UI
{
    public partial class EmployeeForm : Form
    {

        private PersonServiceImpl employeeService;
        private DepartmentService departmentService;
        public EmployeeForm()
        {
            employeeService = new PersonServiceImpl();
            departmentService = new DepartmentService();
            InitializeComponent();
        }

        // 初始化表格数据
        private void dataTableInit()
        {
            // 获取员工数据
            List<Person> list = employeeService.list(new Person());
            // 将数据赋值给容器
            employee_grid_box.DataSource = list;
        }

        //  搜索员工列表
        private void button1_Click(object sender, EventArgs e)
        {

            Person person = new Person();
            person.PersonName = employee_name_box.Text;
            person.IdNum = employee_id_box.Text;
            person.DepartmentName = employee_dept_combo.Text;
            person.IsDel = employee_status.Text;
            Console.WriteLine(person);
            // 获取员工列表信息
            List<Person> list = employeeService.list(person);
            // 将数据赋值给容器
            employee_grid_box.DataSource = list;
        }


        // 打开窗口时执行的方法
        private void EmployeeForm_Load(object sender, EventArgs e)
        {
            this.dataTableInit();
            // 获取部门数据
            List<Department> departments = departmentService.list(new Department());
            employee_dept_combo.SelectedIndex = -1;
            employee_dept_combo.DataSource = departments;
            employee_dept_combo.DisplayMember = "DepartmentName";
            employee_dept_combo.ValueMember = "DepartmentName";
            employee_dept_combo.Text = null;
        }

        // 打开员工新增界面
        private void add_employee_btn_Click(object sender, EventArgs e)
        {
            // 获取部门数据
            List<Department> departments = departmentService.list(new Department());
            add_employee_dept.SelectedIndex = -1;
            add_employee_dept.DataSource = departments;
            add_employee_dept.DisplayMember = "DepartmentName";
            add_employee_dept.ValueMember = "DepartmentName";

            // 将表单之前的残留数据清空
            add_employee_name_box.Text = null;
            add_employee_sex.Text = null;
            add_employee_age.Text = null;
            add_employee_address.Text = null;
            add_employee_phone.Text = null;
            add_employee_dept.Text = null;
            add_employee_id_num.Text = null;
            // 修改group标题
            add_employee_group.Text = "新增员工";
            // 显示新增面板
            employee_panel.Visible = true;
            // 将下面的内容置空，不然是更新操作了
            add_employee_id.Text = null;
        }

        //新增/编辑——取消
        private void employee_cancel_btn_Click(object sender, EventArgs e)
        {
            employee_panel.Visible = false;
        

        }

        // 新增/编辑界面——确定
        private void employee_submit_btn_Click(object sender, EventArgs e)
        {
            try
            {
                // 获取用户输入的员工数据
                Person person = new Person(
                    // 获取最大的id号
                    Convert.ToString(
                        Convert.ToInt32(employeeService.getMaxId()) + 1
                        ),
                    Convert.ToString(add_employee_name_box.Text),
                    Convert.ToString(add_employee_sex.Text),
                    Convert.ToInt32(add_employee_age.Text),
                    Convert.ToString(add_employee_address.Text),
                    Convert.ToString(add_employee_phone.Text),
                    Convert.ToString(add_employee_dept.Text),
                    Convert.ToString(add_employee_id_num.Text),
                    "0");

                bool res = false;
                // 判断是新增还是修改
                if (add_employee_id.Text != null && add_employee_id.Text != "")
                {
                    person.PersonID = add_employee_id.Text;
                    // 调用员工新增业务
                    res = employeeService.updateById(person);
                }
                else
                {
                    // 判断同身份证的员工是否存在
                    Person searchParams = new Person();
                    searchParams.IdNum = Convert.ToString(add_employee_id_num.Text);
                    List<Person> list = employeeService.list(searchParams);
                    if (list.Count > 0)
                    {
                        MessageBox.Show("身份证号码重复！");
                        return;
                    }
                    // 调用员工新增业务
                    res = employeeService.save(person);
                }

                if (res)
                {
                    MessageBox.Show("操作成功！");
                    this.dataTableInit();
                    // panel隐藏
                    employee_panel.Visible = false;
                }
                else
                {
                    MessageBox.Show("添加失败！");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                MessageBox.Show("请填写完整表单信息！");
            }

        }

        // 重置
        private void employee_btn_reset_Click(object sender, EventArgs e)
        {
            // 将表单清空
            employee_name_box.Text = null;
            employee_id_box.Text = null;
            employee_dept_combo.Text = null;
            employee_status.Text = null;
            dataTableInit();
        }

        // 选择路径
        private void select_path_btn_Click(object sender, EventArgs e)
        {
            // 创建文件选择对话框
            FolderBrowserDialog path = new FolderBrowserDialog();
            if (path.ShowDialog() == DialogResult.OK)
            {
                employee_path_box.Text = path.SelectedPath + $"\\员工信息表{new DateTime().Millisecond}.xls";
            }

        }


        // 导出员工信息
        private void employee_export_btn_Click(object sender, EventArgs e)
        {
            if (employee_path_box.Text == null || employee_path_box.Text == "")
            {
                MessageBox.Show("请选择导出路径！");
            }
            else
            {
                // 查询使用员工信息
                List<Person> list = employeeService.list(new Person());
                TOOLS.ExcelUtil<Person> util = new TOOLS.ExcelUtil<Person>();
                // 导出
                util.export(list, employee_path_box.Text.Trim(), "员工信息表");
                MessageBox.Show("导出成功！");
            }
        }

        // 表格单击事件
        private void employee_grid_box_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            var even = employee_grid_box.Columns[e.ColumnIndex];
            Person obj = (Person)employee_grid_box.Rows[e.RowIndex].DataBoundItem;
            // 删除
            if ("employee_delete_btn".Equals(even.DataPropertyName))
            {
                var res = MessageBox.Show("确定删除吗？", "提示", MessageBoxButtons.YesNo);
                var flag = DialogResult.Yes.Equals(res) ? employeeService.deleteById(obj.PersonID) : 0;
                if (flag > 0)
                {
                    MessageBox.Show("删除成功！");
                    // 重新刷新数据
                    dataTableInit();
                }

            }
            // 编辑
            else if ("employee_edit_btn".Equals(even.DataPropertyName))
            {
                // 获取部门数据
                List<Department> departments = departmentService.list(new Department());
                add_employee_dept.SelectedIndex = -1;
                add_employee_dept.DataSource = departments;
                add_employee_dept.DisplayMember = "DepartmentName";
                add_employee_dept.ValueMember = "DepartmentName";

                // 标题修改
                add_employee_group.Text = "修改员工";
                // 回显数据
                add_employee_id.Text = obj.PersonID;
                add_employee_name_box.Text = obj.PersonName;
                add_employee_sex.Text = obj.Gender;
                add_employee_address.Text = obj.Address;
                add_employee_phone.Text = obj.PhoneNum;
                add_employee_dept.Text = obj.DepartmentName;
                add_employee_id_num.Text = obj.IdNum;
                add_employee_age.Text = Convert.ToString(obj.Age);
                employee_panel.Visible = true;
            }
        }
       
    }
}
